package me.xpyex.plugin.fixprimordialpearl;

import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

public class FixEnderIO implements Listener {
    private static final String[] ARMORS = {"item_dark_steel_", "item_end_steel_", "item_stellar_alloy_"};

    @EventHandler
    public void onInvClick(InventoryClickEvent event) {
        if (event.getWhoClicked().getOpenInventory().getTopInventory().getTitle().equalsIgnoreCase("Chest")) {
            PlayerInventory inv = event.getWhoClicked().getInventory();
            checkArmor(event.getWhoClicked(), inv.getHelmet(), inv.getChestplate(), inv.getLeggings(), inv.getBoots());
        }
    }

    @EventHandler
    public void onRightClick(PlayerInteractEvent event) {
        if (event.getPlayer().isSneaking()) {
            PlayerInventory inv = event.getPlayer().getInventory();
            checkArmor(event.getPlayer(), inv.getItemInMainHand(), inv.getItemInOffHand());
        }
    }

    private void checkArmor(HumanEntity player, ItemStack... stacks) {
        for (ItemStack stack : stacks) {
            if (stack == null) continue;
            for (String armor : ARMORS) {
                String checker = String.valueOf(stack.getType()).toLowerCase();
                if (checker.startsWith("EnderIO".toLowerCase()) && checker.contains(armor)) {
                    FixPrimordialPearl.PLAYERS_CHECKING.put(player.getUniqueId(), "无法在铁砧中使用元始珍珠");
                }
            }
        }
    }
}
